package com.cvr.project.utils;

import android.content.Context;

import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\ = /O
 * ____/`---'\____
 * .   ' \\| |// `.
 * / \\||| : |||// \
 * / _||||| -:- |||||- \
 * | | \\\ - /// | |
 * | \_| ''\---/'' | |
 * \ .-\__ `-` ___/-. /
 * ___`. .' /--.--\ `. . __
 * ."" '< `.___\_<|>_/___.' >'"".
 * | | : `- \`.;`\ _ /`;.`/ - ` : | |
 * \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * .............................................
 * 佛祖保佑             永无BUG
 *
 * @author :created by master
 * time :2024/12/27
 * 描述：
 */
public class BiometricUtil {

    private static final BiometricUtil INSTANCE = new BiometricUtil();

    public static BiometricUtil getInstance() {
        return INSTANCE;
    }

    /**
     *返回值见上文的“是否可用的状态码”
     */
    public int isFingerprintAvailable(Context context){
        BiometricManager manager = BiometricManager.from(context);
        return manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK);
    }

    /**
     * 开始验证
     *
     * @param activity
     * @param callBack 验证结果回调
     */
    public void authenticate(FragmentActivity activity, BiometricPrompt.AuthenticationCallback callBack) {
        BiometricPrompt.PromptInfo promptInfo = createUi();
        BiometricPrompt prompt = new BiometricPrompt(activity, ContextCompat.getMainExecutor(activity), callBack);
        prompt.authenticate(promptInfo);
    }

    private BiometricPrompt.PromptInfo createUi() {
        return new BiometricPrompt.PromptInfo.Builder()
                .setTitle("指纹验证")
                .setNegativeButtonText("使用密码登录")
                .build();
    }
}
